<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--片区数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="areaName" placeholder="请输入名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-containers">
          <el-tree :data="areaOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="key" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--小区数据-->
      <el-col :span="20" :xs="24">
        <el-card class="box-card">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="客户编码" prop="customerCode">
              <el-input v-model="queryParams.customerCode" placeholder="请输入客户编码" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <!-- <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                v-hasPermi="['system:customerarchives:add']">新增</el-button>
            </el-col>

            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                @click="handleDelete('批量')" v-hasPermi="['system:customerarchives:remove']">删除</el-button>
            </el-col>


            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row> -->

          <el-table v-loading="loading" :data="userList" border @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="采集时间" align="center" key="gatherTime" prop="gatherTime"
              :show-overflow-tooltip="true" />
            <el-table-column label="阀门状态" align="center" key="valveStatus" prop="valveStatus"
              :show-overflow-tooltip="true" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.valveStatus == '0'">关闭</span>
                <span v-if="scope.row.valveStatus == '1'">开启</span>
                <span v-if="scope.row.valveStatus == '2'">未知</span>
              </template>
            </el-table-column>
            <el-table-column label="类型名称" align="center" key="monitorTypeTitle" prop="monitorTypeTitle"
              :show-overflow-tooltip="true" width="100" />
            <!-- <el-table-column label="类型编码" align="center" key="monitorTypeCode" prop="monitorTypeCode"
              :show-overflow-tooltip="true" width="100" /> -->
            <el-table-column label="客户名称" align="center" key="customerName" prop="customerName"
              :show-overflow-tooltip="true" />
            <el-table-column label="客户编码" align="center" key="customerCode" prop="customerCode"
              :show-overflow-tooltip="true" />
            <el-table-column label="手机号" align="center" key="telephone" prop="telephone" :show-overflow-tooltip="true" />
            <el-table-column label="客户地址" align="center" key="customerAddress" prop="customerAddress"
              :show-overflow-tooltip="true" />
            <!-- <el-table-column label="设备ID" align="center" key="meterId" prop="meterId" :show-overflow-tooltip="true" /> -->
            <el-table-column label="表号" align="center" key="meterCode" prop="meterCode" :show-overflow-tooltip="true" />
            <el-table-column label="内部电压" align="center" key="inBattery" prop="inBattery" :show-overflow-tooltip="true" />
            <el-table-column label="网络信息质量" align="center" key="networkSignalQuality" prop="networkSignalQuality"
              :show-overflow-tooltip="true" width="150" />
            <el-table-column label="外部电池电压" align="center" key="outBattery" prop="outBattery"
              :show-overflow-tooltip="true" width="150" />

            <el-table-column label="状态" align="center" key="status" prop="status" :show-overflow-tooltip="true"
              width="100">
              <template slot-scope="scope">
                <span v-if="scope.row.status == '0'">异常</span>
                <span v-if="scope.row.status == '1'">正常</span>
              </template>
            </el-table-column>
            <el-table-column label="激活状态" align="center" key="activeStatus" prop="activeStatus"
              :show-overflow-tooltip="true" width="100">
              <template slot-scope="scope">
                <span v-if="scope.row.activeStatus == 'N'">未激活</span>
                <span v-if="scope.row.activeStatus == 'Y'">已激活</span>
              </template>
            </el-table-column>
            <!-- <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:customerarchives:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['system:customerarchives:remove']">删除</el-button>
              </template>
            </el-table-column> -->
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

      </el-col>
    </el-row>

    <!-- 添加或修改小区配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="类型名称" prop="monitorTypeTitle" required>
              <el-input v-model="form.monitorTypeTitle" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="编码" prop="customerName" required>
              <el-input v-model="form.customerName" placeholder="请输入编码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设备类型" prop="customerType">
              <!-- <el-select v-model="form.customerType" placeholder="客户类型" clearable style='width:100%'>
                    <el-option key="1" label="民用" value="1" />
                    <el-option key="2" label="商用" value="2" />
                    <el-option key="3" label="公用" value="3" />
                </el-select> -->
              <el-select v-model="form.customerType" filterable placeholder="请选择">
                <el-option v-for="dict in dict.type.customer_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否处理" prop="isAutoSendmsg">
              <el-select v-model="form.isAutoSendmsg" filterable placeholder="请选择">
                <el-option label="是" value="Y"> </el-option>
                <el-option label="否" value="N"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="是否记录" prop="isCloseOverdue">
              <el-select v-model="form.isCloseOverdue" filterable placeholder="请选择">
                <el-option label="是" value="Y"> </el-option>
                <el-option label="否" value="N"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否有效" prop="isOpenPaid">
              <el-select v-model="form.isOpenPaid" filterable placeholder="请选择">
                <el-option label="是" value="Y"> </el-option>
                <el-option label="否" value="N"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="解决方案" prop="customerAddress">
              <el-input v-model="form.customerAddress" placeholder="请输入解决方案" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排序码" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入排序码" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>

import { areaList, areaTreeList } from "@/api/fileManagement/areainfo.js"
import { gettypeList, monitorList, monitorInfo } from "@/api/equipmentMonitoring/equipmentAlarm.js"
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "civilArchives",
  dicts: ["customer_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      idName: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区表格数据
      userList: null,
      // 弹出层标题
      title: "",
      //表计弹出层标题
      title2: "",
      // 片区树选项
      areaOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 片区名称
      areaName: '',
      //是否表计显示弹出层
      bjOPen: false,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "title",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerType: null,
        customerName: null,
        idNumber: null,
        deptId: null,
        openDate: null,
        telephone: null,
        customerCode: null
      },
      // 表单校验
      rules: {
        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" },
        ],
        idNumber: [
          { required: true, message: "证件号不能为空", trigger: "blur" },
        ],
        telephone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],
        customerType: [
          { required: true, message: "客户类型不能为空", trigger: "change" },
        ],
      },
      activeNames: ["1"],
      treeList: [],
      meterList: [],
      supplierList: [],
      schemeList: [],
      customStaus: false,
      areID: null,
    };
  },
  watch: {
    // 根据名称筛选企业树
    areaName(val) {
      this.$refs.tree.filter(val);
    },
    form: {
      deep: true,
      handler(val) {
        this.form = val
      },
    },

  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询企业下拉树结构 */
    getDeptTree() {
      gettypeList().then((response) => {
        let list = []
        let param = {
          children: response.data,
          title: '全部',
          key: '',
        }
        list.push(param)
        this.areaOptions = list
      });
    },
    // 树查询
    treeBlur(event) {
      console.log(event)
    },


    getList() {
      this.loading = true;
      monitorList(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 筛选节点
    filterNode(value, data) {
      console.log(data)
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      if (data.title == '全部') {
        this.queryParams.monitorTypeTitle = '';
      } else {
        this.queryParams.monitorTypeTitle = data.title;
      }

      if (data.type === '2') {
        this.customStaus = true
      } else {
        this.customStaus = false
      }
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.areaId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.idName = selection.map((item) => item.customerName);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.getNumber()
      this.title = "新增报警事项";
      if (this.customStaus) {
        this.form.areaId = this.areID
      } else {
        this.form.areaId = null
      }
    },
    // 刷新客户编码
    refreshNumber() {
      this.getNumber()
    },
    // 获取客户编码
    getNumber() {
      generateNumber('21').then((res) => {
        // this.form.customerCode = res.data
        this.$set(this.form, 'customerCode', res.data)
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.id;
      monitorInfo(userId).then((response) => {
        this.form = response.data;
        this.$set(this.form, "postIds", response.postIds);
        this.$set(this.form, "roleIds", response.roleIds);
        this.open = true;
        this.title = "修改报警事项";
        this.form.password = "";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateArea(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustome(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let userIds = ''
      let userName = ''
      if (row == '批量') {
        userIds = this.ids.join(',')
        userName = this.idName.join(',')
      } else {
        userIds = row.id
        userName = row.customerName
      }
      this.$modal
        .confirm('是否确认删除客户"' + userName + '"的数据？')
        .then(function () {
          return delUser(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style>
.head-containers {
  width: 200px;
  overflow: auto;

  .el-tree {
    min-width: 100%;
    display: inline-block !important;
  }
}

.el-form-item--medium .el-form-item__content {
  line-height: 34px !important;
}

.el-form-item--medium .el-form-item__label {
  line-height: 37px !important;
}
</style>